<?php 
session_start();
$lkdate = $_GET['lkdate']; if (!$lkdate) { header ("location: index.php"); exit;}

include "../../includes/functions.php";
include "../../includes/date.php";

echo db_connect();

 
 /// put auth check here

if ($_GET['sent']) { ////  Process sumbitted data

	if ($_GET['sent'] == 'add') {
		
		//print_r($_GET);
		// in the following, 'type' is either the show date or nothing; if it is nothing it shows every week
		$query = "insert into sponsorship_schedules values ('".$_GET['spot_id']."','".$_GET['show_occurrance_id']."','".$_GET['type']."','0','','".$_GET['time_to_play']."', '".$_GET['order']."')";
		$result = mysql_query($query); if (!$result) { echo "error saving data".$query; exit; }
		header("location: sponsorship_schedule.php?lkdate=$lkdate#".$_GET['show_id']);
		//echo $query;
		
		
	}

if ($_GET['sent'] == 'delete') {
	//echo "hi"; exit;
		//print_r($_GET);
		$query = "delete from sponsorship_schedules where `id` = '".$_GET['id']."'";
		$result = mysql_query($query); if (!$result) echo "error deleting data";
		//echo $query;
		header("location: sponsorship_schedule.php?lkdate=$lkdate");
	}


}

 
new Date();
$date = Date::fromDatetime( $lkdate."T00:00:00" );

$display_date = $date->toString( "%A %B %e, %G" );
$day_of_week = $date->toString( "%A" );

//echo "DATE: $time";
 ?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Sponsorship Schedule</title>
<link href="../../css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../calendar_picker_submit.js"></script>
<script type="text/javascript" src="../../includes/jquery-1.3.2.js"></script>
<script type="text/javascript">
function validateDel (id)  {

	if ( confirm ("Are you sure you want to delete this scheduled spot? There is no way to undo this.") ) {
	window.location = "sponsorship_schedule.php?sent=delete&id="+id+"&lkdate=<?=$lkdate?>";
	} else {  }
	
}
</script>
</head>

<body bgcolor="#eeeeee">


<a href="index.php"><< ADMINISTRATION HOME</> | <a href="sponsorships.php">Sponsorship Spots</a><br /><br />

<table width="750" height="60" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td colspan="4" class="index_table_hed" style="font-size:16px;"><div align="center">SPONSORSHIP SPOT SCHEDULES<br />
 <? echo $display_date ?>  <div>

<form action="sponsorship_schedule.php" name="goToDate" method="GET">
<br /><input name="lkdate" type='hidden'>
<input type=button value="Choose a Different Date" onclick="displayDatePicker('lkdate');">
</form>
</div></div>
</td>
  </tr>
  <tr><td>&nbsp;</td></tr>
  
<?php 


$query='SELECT s.show_id as show_id, sa.id as show_occurrance_id, s.name as name, sa.start_time as time_start, sa.end_time as time_end
FROM show_occurrances sa, shows s
WHERE sa.show_id = s.show_id AND (sa.day_of_week = "'. $day_of_week . '" OR sa.day_of_week = "0000-00-00 00:00:00")  AND s.status = "1"
ORDER BY sa.start_time';
$result= mysql_query($query); //echo $query;

while ($row= mysql_fetch_array($result)) {
$show_id = $row['show_id'];
$show_occurrance_id = $row['show_occurrance_id'];
$show_name = $row['name'];
$show_start = substr ($row['time_start'],0,5);
$show_end = substr ($row['time_end'],0,5);

echo "<tr><td colspan='4'>
<table bgcolor = '#dddddd' width='750'><tr><td>
<a name='$show_id'><b><div class='showName'><a name='$show_id'>$show_name</b> - $show_start - $show_end</div></td></tr>";

	$schedule_query = 'select * from sponsorship_schedules where show_id = "'.$show_occurrance_id.'" and (datetime = "'.$lkdate.'" OR datetime = "0000-00-00 00:00:00") order by `sort`';
	$schedule_result= mysql_query($schedule_query);  //echo $schedule_query;
	$next_sort = 1;
	while ($schedule_row= mysql_fetch_array($schedule_result))  {
		
		$schedule_id = $schedule_row['id'];
		$schedule_spot_id = $schedule_row[0];
		$schedule_sort = $schedule_row['sort']; $next_sort = $schedule_sort+1;
	
		$spot_query = 'select * from sponsorship_spots where id = "'.$schedule_spot_id.'"';
		$spot_result= mysql_query($spot_query);
		
		$spot_row= mysql_fetch_array($spot_result);
		$spot_name = $spot_row[1];
		$spot_number = $spot_row['track_number'];
		$spot_datetime = $schedule_row['datetime'];
		$spot_time_to_play = substr ($schedule_row['time_to_play'],0,5); 
		if ($schedule_row['time_to_play'] != "00:00:00") {$ttp = " (Play at $spot_time_to_play)";}else{$ttp="";}
		//print_r($schedule_row);
	
	if ($e){ 
		$class = "index_table";
	  
		} else { $class = "index_table_empty";  }
	if (substr ($spot_datetime,0,4) == "0000") {
		 $weekly_msg="&nbsp;&nbsp;(Plays every week for this show)";
		 $class = "index_table_repeating";
	   }else{$weekly_msg="";}  
		
	//$myURL = "playlist_new.php?show_id=$show_id&playlist_id=$playlist_id";
	
	//<td width='25%'  onClick=\"window.location.href='$myURL'; \" ><a href='playlist_new.php?show_id=$show_id&playlist_id=$playlist_id'>xxx</a></td>
	if ($spot_number) {$spot_num_text = "<strong>$spot_number</strong> :: ";  }else{$spot_num_text =""; }
	echo "<tr class = '$class' onMouseOver=\"this.className='index_table_o';\" onMouseOut=\"this.className='$class'\">
		<td >• $spot_num_text$spot_name$weekly_msg $ttp</td>
		
		<td width='25' style='padding: 2px;' onClick=\"return validateDel($schedule_id); \">DELETE</td>
	  </tr>
	  ";
	
	}
	echo "<tr><td colspan='4'><div id='button$show_id' style='padding:4px;' class='link'><a onClick=\"$('#button$show_id').hide(); $('#addForm$show_id').fadeIn('slow');\">+ ADD SPOT</a></div>
	<div id='addForm$show_id' class='addSpot' style='display:none;padding:6px;'> Spot to add:
	<form name='form$show_id' action='' method='GET'>
		<select name='spot_id'>";
	
	$spots_query = 'select * from sponsorship_spots order by track_number, title';
		$spots_result= mysql_query($spots_query);
			while ($spots_row= mysql_fetch_array($spots_result)) {
				$spots_id = $spots_row[0];
				$spots_name = $spots_row[1];
				$spots_track_number = $spots_row['track_number'];
				echo "<option value='$spots_id'>$spots_track_number :: $spots_name</option>\n";
			}
			                    
	echo	"</select> &nbsp;&nbsp; for &nbsp;&nbsp;
	<select name='type'><option value='$lkdate'>Just this day's show</option><option value''>Always for this show</option></select>
	<input name=\"show_id\" type=\"hidden\" value=\"$show_id\" />
	<input name=\"show_occurrance_id\" type=\"hidden\" value=\"$show_occurrance_id\" />
	<input name=\"lkdate\" type=\"hidden\" value=\"$lkdate\" />
	<input name=\"sent\" type=\"hidden\" value=\"add\" /> 
	Time to play <input name='time_to_play' type='text' value='' size='4'/> <span style='font-size:9px;'>(optional - format: 24:00)</span>
	Order <input name='order' type='text' value='$next_sort' size='2'/>
	<input name='Add' type='submit' value='Add' />
	</form>
	</div>
	
	</td></tr>
	</table>
	<br />
	</td></tr>
	
	";
}

?>  
  

</table>
<p>&nbsp; </p>
</body>
</html>
